<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="选择项目" prop="projectId">
        <el-select v-model="queryParams.projectId" @change="getSites">
          <el-option v-for="p in projectList" :key="p.id" :label="p.projectName" :value="p.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择场地" prop="siteId">
        <el-select v-model="queryParams.siteId" @change="handleQuery">
          <el-option v-for="s in siteList" :key="s.id" :label="s.name" :value="s.id" />
        </el-select>
<!-- 		<el-input
		  v-model="queryParams.siteName"
		  clearable
		  @keyup.enter="handleQuery"
		/> -->
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预定时间" prop="orderDateRange">
        <el-date-picker
          v-model="orderDateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 表格数据 -->
    <el-table
      v-loading="loading" :data="tableData" :tree-props="{ children: 'sonList'}"
      row-key="id" @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column label="序号" width="80" type="index" /> -->
      <el-table-column label="场地" prop="siteName" min-width="200" />
      <el-table-column label="项目" prop="projectName" min-width="120" />
      <el-table-column label="预订时间" min-width="220">
        <template #default="scope">
          <div v-if="scope.row.parentType!='1'">
            <span class="mr10">{{ scope.row.orderDate }}</span>
            <span>{{ scope.row.orderTimePeriod.split(',').join('~') }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" prop="orderCode" min-width="180" />
      <el-table-column label="锁场时长（小时）" prop="orderHour" min-width="140" align="center">
		<template #default="scope">
			<span v-if="scope.row.orderHour=='24'&&scope.row.parentType!='1'">全天</span>
			<span v-else>{{scope.row.orderHour}}</span>
		</template>
	  </el-table-column>
      <el-table-column label="状态" prop="statusStr" min-width="140" />
	  <el-table-column label="备注" prop="remark" min-width="200" />
      <el-table-column label="操作员" prop="operatorName" min-width="120" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding" min-width="120">
        <template #default="scope">
          <el-button type="text" @click="viewRow(scope.row)">详情</el-button>
          <el-button type="text" @click="edit(scope.row)" v-if="scope.row.status=='1'">修改</el-button>
          <el-button type="text" @click="unlock(scope.row)" v-if="scope.row.status=='1'">解锁</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <!--详情-->
    <dialogViewReserve :props="dialogViewProps" />
    <!--修改-->
    <!-- <dialogEdit :props="dialogEditProps" @submitForm="getList" /> -->
	<!-- :project-id="props.projectId" -->
	<dialogEdit :props="dialogEditProps"  @submitForm="getList" />
  </div>
</template>

<script setup>
import { getOrderList,unSubscribeOrder} from '@/api/ven/reserve'
import { onMounted, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import { getCurrentInstance } from 'vue'
import { getSiteList, projectConfigList } from '@/api/ven'
import { useRouter } from 'vue-router'
import dialogViewReserve from './components/dialog/viewReserve'
import dialogEdit from './components/dialog/lockEdit'

import { dayjs } from 'element-plus'
import { decimalTo2 } from '@/utils/ruoyi'


const router = useRouter()
const { proxy } = getCurrentInstance()

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderType: '3',
    isParent: '1'
  },
  showSearch: true,
  loading: false,
  tableData: [],
  total: 0,
  selectedIds: [],
  multiple: true,
  projectList: [],
  siteList: [],
  orderDateRange: [],
  open_Lock: false
})
const { queryParams, showSearch, loading, tableData, total, selectedIds, multiple, projectList, siteList, orderDateRange,open_Lock } = toRefs(data)
const props = defineProps({
  projectId: {
    type: String,
    default: null,
    required: true
  }
})
const dialogPropsBase = {
  open: false,
  data: null
}
const dialogViewProps = reactive({
  ...dialogPropsBase
})
const dialogEditProps = reactive({
  ...dialogPropsBase
})


onMounted(() => {
  projectConfigList().then(res => {
    projectList.value = res.rows
  })
	getSiteList().then(res => {
	  siteList.value = res.rows
	})
  getList()
})

function getSites() {
  queryParams.value.siteId = ''
  siteList.value = []
  getSiteList({
    projectId: queryParams.value.projectId
  }).then(res => {
    siteList.value = res.rows
  })

  handleQuery()
}


function getList() {
  loading.value = true
  getOrderList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  if (orderDateRange.value.length > 0) {
    queryParams.value.orderDateRange = orderDateRange.value.join(',')
  }
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  orderDateRange.value = []
  handleQuery()
}


function handleSelectionChange(selection) {
  selectedIds.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

function viewRow(row) {
  _.assign(dialogViewProps, dialogPropsBase, {
    open: true,
    data: { ...row }
  })
}

function edit(row) {
  _.assign(dialogEditProps, dialogPropsBase, {
    open: true,
    data: { ...row }
  })
}
function unlock(row){
	const title = `确定解锁 ${row.siteName}？`
	proxy.$modal.confirm(title)
	    .then(() => {
	      unSubscribeOrder(row.id, { data: 0 }).then(() => {
	        proxy.$modal.msgSuccess('操作成功')
	        setTimeout(handleQuery(), 1000)
	    })
	})

}


</script>

<style scoped>
	.mr10{margin-right:10px;}
</style>
